/**
 * 
 */
package com.mixsmart.core.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;

import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mixsmart.core.bean.entity.IEntityBean;
import com.mixsmart.core.dao.IBaseDao;
import com.mixsmart.core.enums.DBType;
import com.mixsmart.spellsql.ICondition;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.LoggerUtils;
import com.mixsmart.utils.SQLUtils;

/**
 * 通用DAO实现类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2015年12月21日
 */
public class BaseDao extends AbstractQueryDao implements IBaseDao {

	private static final Logger logger = LoggerFactory.getLogger(BaseDao.class);
	
	@Override
	public DBType getDatabaseType() {
		// TODO 实现IBaseDao中的方法
		String strDialect = ((SessionImpl) getSession()).getFactory().getDialect().getClass().getName();
		strDialect = strDialect.toLowerCase();
		DBType type = DBType.MYSQL;
		if(strDialect.contains("oracle")) {
			type = DBType.ORACLE;
		} else if(strDialect.contains("mysql")) {
			type = DBType.MYSQL;
		} else if(strDialect.contains("sqlserver")) {
			type = DBType.SQLSERVER;
		} else if(strDialect.contains("postgresql")) {
			type = DBType.PostgreSQL;
		}
		return type;
	}

	@Override
	public boolean updateObj(IEntityBean bean) {
		// TODO 实现IUpdateDao中的方法
		boolean is = false;
		LoggerUtils.debug(logger, "更新数据["+bean.toString()+"]");
		if(null != bean ) {
			super.getSession().update(bean);
			LoggerUtils.debug(logger, "更新数据--成功");
			is = true;
		}
		return is;
	}

	@Override
	public boolean updateObj(ArrayList<? extends IEntityBean> beans) {
		// TODO 实现IUpdateDao中的方法
		boolean is = false;
		LoggerUtils.debug(logger, "批量更新数据");
		if(CollectionUtils.isNotEmpty(beans)) {
			Session session = super.getSession();
			for (IEntityBean t : beans) {
				session.update(t);
			}
			LoggerUtils.debug(logger, "批量更新数据--成功");
			is = true;
		} else {
			LoggerUtils.debug(logger, "批量更新数据--失败[列表为空]");
		}
		return is;
	}

	@Override
	public boolean updateSql(String sql, HashMap<String, Object> params) {
		// TODO 实现IUpdateDao中的方法
		boolean is = false;
		if(SQLUtils.isUpdate(sql)) {
			is = super.executeSQL(sql, params, false);
		} else {
			LoggerUtils.error(logger, "SQL语句不是更新语句...["+sql+"]...");
		}
		return is;
	}
	
	@Override
	public boolean updateSqlFilter(String sql, HashMap<String, Object> params) {
		// TODO 实现IUpdateDao中的方法
		boolean is = false;
		if(SQLUtils.isUpdate(sql)) {
			is = super.executeSQL(sql, params, true);
		} else {
			LoggerUtils.error(logger, "SQL语句不是更新语句...["+sql+"]...");
		}
		return is;
	}

	@Override
	public boolean delete(Class<? extends IEntityBean> clazz, ICondition condition) {
		// TODO 实现IDeleteDao中的方法
		boolean is = false;
		LoggerUtils.debug(logger, "按条件删除数据HQL...");
		if(null == clazz) {
			return is;
		}
		if(null != condition) {
			String hql = "delete from "+clazz.getName()+" "+condition.build();
			LoggerUtils.debug(logger, "删除HQL["+hql+"]");
			is = super.executeHQL(hql, condition.param(), false);
		}
		return is;
	}

	@Override
	public boolean deleteSql(String sql, HashMap<String, Object> params) {
		// TODO 实现IDeleteDao中的方法
		LoggerUtils.debug(logger, "按SQL语句删除数据");
		boolean is = false;
		if(SQLUtils.isDelete(sql)) {
			is = super.executeSQL(sql, params, false);
		} else {
			LoggerUtils.error(logger, "SQL语句不是删除语句...["+sql+"]...");
		}
		return is;
	}
	
	@Override
	public boolean deleteSqlFilter(String sql, HashMap<String, Object> params) {
		// TODO 实现IDeleteDao中的方法
		LoggerUtils.debug(logger, "按SQL语句删除数据");
		boolean is = false;
		if(SQLUtils.isDelete(sql)) {
			is = super.executeSQL(sql, params, true);
		} else {
			LoggerUtils.error(logger, "SQL语句不是删除语句...["+sql+"]...");
		}
		return is;
	}

	@Override
	public boolean saveObj(IEntityBean bean) {
		// TODO 实现ISaveDao中的方法
		boolean is = false;
		LoggerUtils.debug(logger, "保存数据["+bean.toString()+"]");
		if(null != bean) {
			super.getSession().save(bean);
			is = true;
			LoggerUtils.debug(logger, "保存数据--成功");
		}
		return is;
	}

	@Override
	public boolean saveObj(ArrayList<? extends IEntityBean> beans) {
		// TODO 实现ISaveDao中的方法
		boolean is = false;
		LoggerUtils.debug(logger, "批量保存数据");
		if(CollectionUtils.isNotEmpty(beans)) {
			Session session = super.getSession();
			for (IEntityBean t : beans) 
				session.save(t);
			LoggerUtils.debug(logger, "批量保存数据--成功");
			is = true;
			beans = null;
		} else {
			LoggerUtils.debug(logger, "批量保存数据--失败[列表为空]");
		}
		return is;
	}

	@Override
	public boolean saveOrUpdateObj(IEntityBean bean) {
		boolean is = false;
		LoggerUtils.debug(logger, "保存或更新数据["+bean.toString()+"]");
		if(null != bean) {
			super.getSession().saveOrUpdate(bean);
			is = true;
			LoggerUtils.debug(logger, "保存或更新--成功");
		}
		return is;
	}

	@Override
	public boolean insertSql(String sql, HashMap<String, Object> params) {
		boolean is = false;
		if(SQLUtils.isInsert(sql)) {
			is = super.executeSQL(sql, params, false);
		} else {
			LoggerUtils.error(logger, "SQL语句不是插入语句...["+sql+"]...");
		}
		return is;
	}

	@Override
	public boolean insertSqlFilter(String sql, HashMap<String, Object> params) {
		boolean is = false;
		if(SQLUtils.isInsert(sql)) {
			is = super.executeSQL(sql, params, true);
		} else {
			LoggerUtils.error(logger, "SQL语句不是插入语句...["+sql+"]...");
		}
		return is;
	}

	@Override
	public boolean deleteHql(String hql, HashMap<String, Object> params) {
		boolean is = false;
		if(SQLUtils.isDelete(hql)) {
			is = super.executeHQL(hql, params, false);
		} else {
			LoggerUtils.error(logger, "该HQL语句不是删除语句...["+hql+"]...");
		}
		return is;
	}

	@Override
	public boolean deleteHqlFilter(String hql, HashMap<String, Object> params) {
		boolean is = false;
		if(SQLUtils.isDelete(hql)) {
			is = super.executeHQL(hql, params, true);
		} else {
			LoggerUtils.error(logger, "该HQL语句不是删除语句...["+hql+"]...");
		}
		return is;
	}

	@Override
	public boolean updateHql(String hql, HashMap<String, Object> params) {
		boolean is = false;
		if(SQLUtils.isUpdate(hql))
			is = super.executeHQL(hql, params, false);
		else 
			LoggerUtils.error(logger, "该HQL语句不是更新语句...["+hql+"]...");
		return is;
	}

	@Override
	public boolean updateHqlFilter(String hql, HashMap<String, Object> params) {
		boolean is = false;
		if(SQLUtils.isUpdate(hql))
			is = super.executeHQL(hql, params, true);
		else 
			LoggerUtils.error(logger, "该HQL语句不是更新语句...["+hql+"]...");
		return is;
	}
	
	
}
